
package com.hqyj.controller;

import com.hqyj.dao.HouseDao;
import com.hqyj.entity.House;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/jpahouse")
public class JPAHouseController {
   @Autowired
    HouseDao houseDao;

    @GetMapping
    @RequestMapping(value = "/house2")
    String houses1(Model m, Integer pageNum){
        if (pageNum == null){
            pageNum = 1;
        }
//        PageRequest pageReques=PageRequest.of(pageNum-1,3);   //获取第1页的两条记录
//        Sort sort = Sort.by(Sort.Direction.DESC, "house_id");
        Pageable pageable = PageRequest.of(pageNum-1,2);
        Page<House> page=houseDao.findAll(pageable);
//        List<House> houses=page.getContent();
//        for(House t:houses) {
//            System.out.println(t);
//            houses.add(t);
//        }
//        m.addAttribute("pageInfo", houses);

        m.addAttribute("houses",page);
        return "restful/houseList";
    }



    @RequestMapping("/deletehouse/{id}")
    @ResponseBody
    String deleteByHouse(@PathVariable int id) {
       houseDao.deleteById(id);
       return "success";

   }
}
